'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
#作者：cacho_37967865
#博客：https://blog.csdn.net/sinat_37967865
#文件：deal_list.py
#日期：2019-11-19
#备注：本文主要介绍了python中列表拆分、合并    
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

from collections import Counter   #引入Counter

# 1维列表转换为字符串
def list_to_str(org_list):
    new_str = ','.join(map(str, org_list))   # map可以对int类型进行转换，',' 分隔符
    #print(new_str)
    return new_str

# 拆分列表：原始列表、拆分长度
def split_list(org_list,length):
    n = len(org_list)
    new_list = []
    for i in range(0, n, length):
        new_list.append(org_list[i:i + length])
    print(new_list)
    return new_list


# 合并列表的元素(相同维度)
def merge_list_element(one_list,two_list):
    new_list = one_list + two_list
    #one_list.extend(two_list)
    print(new_list)
    #print(one_list)
    return new_list


# 合并列表成 +1 列表
def merge_list(one_list,two_list):
    new_list = []
    new_list.append(one_list)
    new_list.append(two_list)
    print(new_list)
    return new_list


# 判断列表是否存在某个字段
def in_not_list(str_word,base_list):
    if str_word not in base_list:
        print(str_word)


# 2维数组修改为一维数组
def change_list(two_list):
    one_list = []
    for a in two_list:
        for b in a:
            one_list.append(b)
    #print(one_list)
    return(one_list)

# 1维数组去重，并且找到重复次数,r_type 得到一个字典 'd' 或2维列表 'l'
def clear_list(one_list,r_type):
    b = dict(Counter(one_list))
    #dict_repeat = {key: value for key, value in b.items() if value > 1}  # 展现重复元素和重复次数
    dict_count = {key: value for key, value in b.items()}                 # 展现去重后元素和其出现次数
    # 字典类型dict
    if r_type =='d':
    #print(dict_count)
        return dict_count
    # 二维列表
    elif r_type =='l':
        list_count = []
        for key in dict_count:
            row = []
            row.append(key)
            row.append(dict_count[key])
            list_count.append(row)
        return list_count
    else:
        print('无此类型的数据！')
        return(None)


if __name__ == '__main__':
    org_list = [1, 2, 3, 4, 5, 6]
    two_list = [[1,3],[2,4]]
    #split_list(org_list, 4)
    #merge_list_element(org_list, two_list)
    #merge_list(org_list, two_list)
    #change_list(two_list)
    #clear_list(['s', 'b', 'c', 'd', 'e', 'c', 'a', 'd', 'd', 'c', 'c', 't', 'a', 'a'])
    list_to_str(org_list)